	
		$(document).on('pageshow', "#indexPage",function () {

					var page = window.location.pathname; 
		
			if (localStorage.xmlFile == undefined && page.indexOf('configuration.html') <= 0){
				window.location.href = "configuration.html";
			}		
			
					$('#about').text(localStorage.aboutLabel);
			$('#back').text(localStorage.backLabel);
			$('#home').text(localStorage.homeLabel);
			$('#search').text(localStorage.searchLabel);
			$('#appTitle').text(localStorage.titleLabel);
			$('#configuration').text(localStorage.configLabel);
			$('#indexSearch').empty();
			$('#indexSearch').val('');
			loadCategories();
        }); 
		
		$(document).on('pageshow', "#searchPage",function () {

					$('#about').text(localStorage.aboutLabel);
			$('#back').text(localStorage.backLabel);
			$('#home').text(localStorage.homeLabel);
			$('#search').text(localStorage.searchLabel);
			$('#appTitle').text(localStorage.titleLabel);
			$('#configuration').text(localStorage.configLabel);
			$('#listResultTitle').hide();
			loadSearch();		             

        }); 
		
		$(document).on('pageshow', "#itemsPage",function () {
				
					$('#about').text(localStorage.aboutLabel);
			$('#back').text(localStorage.backLabel);
			$('#home').text(localStorage.homeLabel);
			$('#search').text(localStorage.searchLabel);
			$('#appTitle').text(localStorage.titleLabel);
			$('#configuration').text(localStorage.configLabel);
				$('#itemsList').hide();
				loadQuestions();		             

        });
		
		$(document).on('pageshow', "#contentPage",function () {
	
			$('#answerContent').hide();
			loadContent();	             
			$('#back').text(localStorage.backLabel);
			$('#home').text(localStorage.homeLabel);
        });
		
		$(document).on('pageshow', "#aboutPage",function () {
			$('#back').text(localStorage.backLabel);
			$('#home').text(localStorage.homeLabel);
        });
		

		function loadSearch(){
			
			if (localStorage.searchedText != "")
			{
				$('#searchInput').val(localStorage.searchedText);
				search();			
			}
		}
		
	 function loadCategories(){
	 
	 	var menuList = $('#categoriesList');
	 	
	 		
			menuList.hide();
			menuList.empty();
			
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			
			var xmlFile = localStorage.getItem("xmlFile");
			xmlhttp.open("GET", xmlFile, false);
			xmlhttp.send();
			xmlDoc=xmlhttp.responseXML;

			var x = xmlDoc.getElementsByTagName("category");
			
			var liText1 = "<li><a onClick=$.mobile.changePage('itemsMenu.html?selectedItem=";
			var liText2 = "',{}); >";
			
			for (i=0;i<x.length;i++)
				  {
				 
				  var name = x[i].getElementsByTagName("categoryName")[0].childNodes[0].nodeValue;
				  var id = x[i].getElementsByTagName("categoryId")[0].childNodes[0].nodeValue;
				  
				  menuList.append( liText1 + id + liText2 + name + "</a></li><br />");

				  }
			menuList.show();	  
			menuList.listview('refresh');
		};
		
		
		function loadQuestions() {
			var parameters = location.search.substring(1).split("&");
			var temp = parameters[0].split("=");
			nodeName = unescape(temp[1]);

			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp = new XMLHttpRequest();
			}
			else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			var xmlFile = localStorage.getItem("xmlFile");
			xmlhttp.open("GET", xmlFile, false);
			xmlhttp.send();
			xmlDoc = xmlhttp.responseXML;
		
			var categoryList = $('#itemsList');
			categoryList.empty();
		
			var x = xmlDoc.getElementsByTagName("category");
			for (i = 0; i < x.length; i++) {
				if (x[i].getElementsByTagName("categoryId")[0].childNodes[0].nodeValue == nodeName) {
					$('#CategoryName').text(x[i].getElementsByTagName("categoryName")[0].childNodes[0].nodeValue);
					var y = x[i].getElementsByTagName("item");
					for (j = 0; j < y.length; j++) {
								   
						var name = y[j].getElementsByTagName("title")[0].childNodes[0].nodeValue;
						var id = y[j].getElementsByTagName("id")[0].childNodes[0].nodeValue; 
						categoryList.append("<li><a onClick=$.mobile.changePage('contentViewer.html?selectedItem=" + id + "',{}); >" + name + "</a></li><br />");			
					}
				}

			}
			categoryList.show();
		$('ul').listview('refresh');
		};
	
	function loadContent(){

        var parameters = location.search.substring(1).split("&");
        var temp = parameters[0].split("=");
        nodeName = unescape(temp[1]);

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var xmlFile = localStorage.getItem("xmlFile");
		xmlhttp.open("GET", xmlFile, false);
        xmlhttp.send();
        xmlDoc = xmlhttp.responseXML;

        var x = xmlDoc.getElementsByTagName("item");
        for (i = 0; i < x.length; i++) {
            if (x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue == nodeName) {
				$('#ItemName').text(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
                var description = x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
				$('#answerContent').html(description);                
			}
		}
		$('#answerContent').show();

	};
	
	
	function searchEvent(){
			if (window.event && window.event.keyCode == 13 && $('#searchInput').textinput().val().length >= 3) {
				localStorage.setItem("searchedText",  $('#searchInput').textinput().val());
				search();
			}
		};
		
		
	function search(){
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		var xmlFile = localStorage.getItem("xmlFile");
		xmlhttp.open("GET", xmlFile, false);
		xmlhttp.send();
		xmlDoc=xmlhttp.responseXML;
		
		$('#listResultTitle').hide();
		$('#listResultContent').hide();
		
		$('#listResultTitle').empty();
		$('#listResultContent').empty();
		
		var items = xmlDoc.getElementsByTagName("item");
		
		var searchedText = $('#searchInput').textinput().val().toLowerCase();
		
		for (i=0;i<items.length;i++)
			  {				 
				var name = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
				if (name.toLowerCase().indexOf(searchedText) >= 0)
				{
					var id = items[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;				  
					$('#listResultTitle').append("<li><a onClick=$.mobile.changePage('contentViewer.html?selectedItem=" + id + "',{}); >"  + name + "</a></li><br />");					
				}
				else
				{
					var description = items[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
					if (description.toLowerCase().indexOf(searchedText) >= 0)
					{
						var id = items[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;				  
						$('#listResultContent').append("<li><a onClick=$.mobile.changePage('contentViewer.html?selectedItem=" + id + "',{}); >"  + name + "</a></li><br />");					
					}
				}
			  }
		$('#listResultTitle').listview('refresh');
		$('#listResultContent').listview('refresh');
		
		$('#listResultTitle').show();
		$('#listResultContent').show();
	};	
	
		function configSpanish() {
			localStorage.clear();
			localStorage.setItem("xmlFile", "guiaEsp.xml");			
			localStorage.setItem("aboutLabel", "Acerca de");	
			localStorage.setItem("backLabel", "Volver");	
			localStorage.setItem("homeLabel", "Inicio");	
			localStorage.setItem("searchLabel", "Buscar");	
			localStorage.setItem("titleLabel", "Guía para familiares de personas con la enfermedad de Alzheimer");	
			localStorage.setItem("configLabel", "Configuración");
			
			$.mobile.changePage( 'index.html', {  });				
		}	
		
		function configEnglish() {
			localStorage.clear();
			localStorage.setItem("xmlFile", "guiaEng.xml");			
			localStorage.setItem("aboutLabel", "About us");	
			localStorage.setItem("backLabel", "Back");	
			localStorage.setItem("homeLabel", "Home");	
			localStorage.setItem("searchLabel", "Search");
			localStorage.setItem("titleLabel", "Guide for relatives of people with Alzheimer's disease");	
			localStorage.setItem("configLabel", "Configuration");
			
			$.mobile.changePage( 'index.html', {  });				
		}	
		
				
		function indexSearch() {
			if (window.event && window.event.keyCode == 13 && $('#indexSearch').val().length >= 3) {
				localStorage.setItem("searchedText", $('#indexSearch').val());
				$.mobile.changePage( 'search.html', {  });
			}
		}	